﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.EwesGiveBirthes
{
    public class EwesGiveBirthQueryHandler : IRequestHandler<EwesGiveBirthQueryCommand, ResultPage<EwesGiveBirthQueryDto>>
    {
        private readonly IBaseRepository<EwesGiveBirth> _ewesGiveBirthRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IMapper _mapper;

        public EwesGiveBirthQueryHandler(IBaseRepository<EwesGiveBirth> ewesGiveBirthRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IMapper mapper)
        {
            _ewesGiveBirthRepository = ewesGiveBirthRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _mapper = mapper;
        }


        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<EwesGiveBirthQueryDto>> Handle(EwesGiveBirthQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<EwesGiveBirthQueryDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var ewesGiveBirth = await(from a in await _ewesGiveBirthRepository.GetAll()
                                          join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                          join c in await _fieldRepository.GetAll() on a.FieldId equals c.FieldId
                                          where a.IsDeleted == false
                                          select new EwesGiveBirthQueryDto()
                                          {
                                              EwesGiveBirthId = a.EwesGiveBirthId,
                                              EarBugles = a.EarBugles,
                                              PregnancyDays = a.PregnancyDays,
                                              ParturitionDifficulty = a.ParturitionDifficulty,
                                              FatSheepBatch = a.FatSheepBatch,
                                              MaternalInstinct = a.MaternalInstinct,
                                              Lactation = a.Lactation,
                                              Mastitis = a.Mastitis,
                                              NestNumber = a.NestNumber,
                                              HealthyLambNums = a.HealthyLambNums,
                                              WeakLambsNums = a.WeakLambsNums,
                                              DeformityNums = a.DeformityNums,
                                              StillbirthNums = a.StillbirthNums,
                                              MummificationNums = a.MummificationNums,
                                              TeratogenesiNums = a.TeratogenesiNums,
                                              LivingLambs = a.LivingLambs,
                                              LitterWeight = a.LitterWeight,
                                              Sex = a.Sex,
                                              CurrentBuildings = a.CurrentBuildings,
                                              CurrentColumns = a.CurrentColumns,
                                              CottageId = b.CottageId,
                                              CottageName = b.CottageName,
                                              FieldId = c.FieldId,
                                              FieldName = c.FieldName,
                                              EwesGiveBirthTime = a.EwesGiveBirthTime,
                                              CreatedOwnerId = a.CreatedOwnerId,
                                              CreatedTime = a.CreatedTime,
                                          }).ToListAsync();
                if (request.StartEwesGiveBirthTime != null)
                {
                    ewesGiveBirth = ewesGiveBirth.Where(c => c.EwesGiveBirthTime >= request.StartEwesGiveBirthTime).ToList();
                }
                if (request.EndEwesGiveBirthTime != null)
                {
                    ewesGiveBirth = ewesGiveBirth.Where(c => c.EwesGiveBirthTime <= request.EndEwesGiveBirthTime).ToList();
                }
                result.TotalCount = ewesGiveBirth.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                ewesGiveBirth = ewesGiveBirth.OrderBy(c => c.EwesGiveBirthId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = ewesGiveBirth;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
